﻿using JYH.Data;
using JYH.DataAccess;
using JYH.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JYH.Business
{
    public class WmsStocktakeOrderInfoBLL : BusinessBase<WmsStocktakeOrderInfo>
    {

        public WmsStocktakeOrderInfoBLL()
        {
            SqlSelect = "Select WmsStocktakeOrderInfo.* From WmsStocktakeOrderInfo ";
            myDataBase = DB.D();
        }
        /// <summary>
        /// 盘点单同步
        /// </summary>
        /// <param name="Orders"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static bool SyncStockTakeOrder(List<WmsStocktakeOrderInfo> Orders, out string Msg)
        {
            Msg = string.Empty;
            Database db = DB.D();
            db.BeginTransaction();
            StringBuilder sb = new StringBuilder();
            Sql sql = new Sql();
            Sql whereSql = new Sql();
            try
            {

                sql = new Sql();
                sql.Append(@"Select WmsStocktakeOrderInfo.*
                            From WmsStocktakeOrderInfo
                         ");
                for (int i = 0; i < Orders.Count; i++)
                {
                    if (i == 0)
                    {

                        whereSql.Append(" Where ( WmsStocktakeOrderInfo.WarehouseNO='" + Orders[i].WarehouseNO + "' And WmsStocktakeOrderInfo.TakeType='" + Orders[i].TakeType + "')");
                    }
                    else
                    {
                        whereSql.Append(" Or ( WmsStocktakeOrderInfo.WarehouseNO='" + Orders[i].WarehouseNO + "' And WmsStocktakeOrderInfo.TakeType='" + Orders[i].TakeType + "')");
                    }
                }
                List<WmsStocktakeOrderInfo> OldOrders = new List<WmsStocktakeOrderInfo>();
                sql.Append(whereSql.SQL);
                sql.OrderBy("WmsStocktakeOrderInfo.StocktakeNO Desc");
                OldOrders = db.Fetch<WmsStocktakeOrderInfo>(sql.SQL);
                int count = 0;
                for (int i = 0; i < Orders.Count; i++)
                {
                    int index = -1;
                    index = OldOrders.FindIndex(p => p.TakeType == Orders[i].TakeType && p.WarehouseNO == Orders[i].WarehouseNO);
                    if (index < 0)
                    {
                        WmsStocktakeOrderInfo obj = new WmsStocktakeOrderInfo();
                        obj.WarehouseNO = Orders[i].WarehouseNO;
                        obj.TakeType = Orders[i].TakeType;
                        obj.RState = "0";
                        obj.StocktakeID = Guid.NewGuid().ToString();
                        obj.StocktakeNO = SysFormNOInfoBLL.GetLastFormNO("StockTakeOrder", out Msg);
                        obj.CreateTime = BusinessTool.GetDateTime();
                        obj.CreateUser = "Admin";
                        sb.Append(BusinessTool.GetInsertSql(obj));
                        for (int j = 0; j < Orders[i].TakeMaterials.Count; j++)
                        {
                            WmsStocktakeMaterial m = new WmsStocktakeMaterial();
                            m.BatchNO = Orders[i].TakeMaterials[j].BatchNO;
                            m.StocktakeID = obj.StocktakeID;
                            m.ErpItemNO = Orders[i].TakeMaterials[j].ErpItemNO;
                            m.InvQty = Orders[i].TakeMaterials[j].InvQty;
                            m.MaterialNO = Orders[i].TakeMaterials[j].MaterialNO;
                            m.StorageNO = Orders[i].TakeMaterials[j].StorageNO;
                            m.WarehouseNO = Orders[i].TakeMaterials[j].WarehouseNO;
                            m.UnitNO = Orders[i].TakeMaterials[j].UnitNO;
                            m.TakeMaterialID = Guid.NewGuid().ToString();
                            m.RState = "0";
                            sb.Append(BusinessTool.GetInsertSql(m));
                            count++;
                        }


                    }
                    else
                    {
                        if (string.Equals(OldOrders[index].RState, "0"))
                        {
                            sb.Append(" Delete WmsStocktakeMaterial Where WmsStocktakeMaterial.StocktakeID='" + OldOrders[index].StocktakeID + "'");
                        }
                        for (int j = 0; j < Orders[i].TakeMaterials.Count; j++)
                        {
                            WmsStocktakeMaterial m = new WmsStocktakeMaterial();
                            m.BatchNO = Orders[i].TakeMaterials[j].BatchNO;
                            m.StocktakeID = OldOrders[index].StocktakeID;
                            m.ErpItemNO = Orders[i].TakeMaterials[j].ErpItemNO;
                            m.InvQty = Orders[i].TakeMaterials[j].InvQty;
                            m.MaterialNO = Orders[i].TakeMaterials[j].MaterialNO;
                            m.StorageNO = Orders[i].TakeMaterials[j].StorageNO;
                            m.WarehouseNO = Orders[i].TakeMaterials[j].WarehouseNO;
                            m.UnitNO = Orders[i].TakeMaterials[j].UnitNO;
                            m.TakeMaterialID = Guid.NewGuid().ToString();
                            m.RState = "0";
                            sb.Append(BusinessTool.GetInsertSql(m));
                            count++;
                        }
                    }
                    if (count > 500)
                    {
                        db.Execute(sb.ToString());
                        sb = new StringBuilder();
                        count = 0;
                    }
                }



                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                }
                db.CompleteTransaction();
                return true;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 依据盘点单号或盘点日期或盘点类型查询盘点单
        /// </summary>
        /// <param name="Order">StocktakeOrder对象</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public List<WmsStocktakeOrderInfo> GetStockTakeOrderList (WmsStocktakeOrderInfo Order, out long Total, out string Msg )
       {
         
            Msg =String.Empty;
            Total = 0;
            try
            {
                Sql sql = new Sql();
                sql.Append(this.SqlSelect);
                if (!string.IsNullOrEmpty(Order.WarehouseNO))
                {
                    sql.Where("WmsStocktakeOrderInfo.WarehouseNO='" + Order.WarehouseNO + "'");
                }
                if (!string.IsNullOrEmpty(Order.TakeType))
                {
                    sql.Where("WmsStocktakeOrderInfo.TakeType='" + Order.TakeType + "'");
                }
                if (!string.IsNullOrEmpty(Order.StocktakeNO))
                {
                    sql.Where("WmsStocktakeOrderInfo.StocktakeNO Like '%" + Order.StocktakeNO + "%'");
                }
                sql.OrderBy("WmsStocktakeOrderInfo.StocktakeNO");
                Page<WmsStocktakeOrderInfo> Page = myDataBase.Page<WmsStocktakeOrderInfo>(Order.PageIndex, Order.PageSize, sql);
                Total = Page.TotalPages;
                return Page.Items;
            }
            catch (Exception ex)
            { 
                Msg=ex.ToString();
                return null;
            }
           
        }
        /// <summary>
        /// 条码盘点
        /// </summary>
        /// <param name="BarcodeNO">物料条码</param>
        /// <param name="TaskType">盘点步骤 1 初盘 2 复盘 3 终盘</param>
        /// <param name="StockTakeNO">盘点单号</param>
        /// <param name="Msg"></param>
        /// <returns>相同储位+料号的条码清单（盘点步骤相同）</returns>
        public List<WmsStocktakeBarcode> TakeBarcode(string BarcodeNO, string StorageNO, string LampNO, string TaskType, string StockTakeNO, string UserNO, out string Msg)
        {

            List<WmsStocktakeBarcode> TakeBarcodes = new List<WmsStocktakeBarcode>();
            StringBuilder sb = new StringBuilder();
           Msg = String.Empty;
            Database db = DB.D();
            Sql sql = new Sql();
            BasBarcodeInfo Barcode=new BasBarcodeInfo();
            db.BeginTransaction();
            try
            {


                sql = new Sql();
                sql.Append(@"Select WmsStocktakeOrderInfo.*
                            From WmsStocktakeOrderInfo
                           
                            Where WmsStocktakeOrderInfo.StocktakeNO='" + StockTakeNO + "'");
                WmsStocktakeOrderInfo OrderInfo=db.FirstOrDefault<WmsStocktakeOrderInfo>(sql.ToString());
                if (OrderInfo == null)
                {
                    Msg = " Order NO Is Error";
                    db.AbortTransaction();
                    return null;
                }
                

                sql = new Sql();
                sql.Append(@"Select WmsStocktakeMaterial.*,
                            WmsStocktakeOrderInfo.StocktakeNO,
                            WmsStocktakeOrderInfo.TakeType
                            From WmsStocktakeMaterial
                            Inner JOin WmsStocktakeOrderInfo 
                            ON WmsStocktakeMaterial.StocktakeID=WmsStocktakeOrderInfo.StocktakeID
                            And WmsStocktakeOrderInfo.StocktakeNO='" + StockTakeNO + "'");
                sql.OrderBy("WmsStocktakeMaterial.ErpItemNO");
                List<WmsStocktakeMaterial> StocktakeMaterials=db.Fetch<WmsStocktakeMaterial>(sql);


                if (StocktakeMaterials == null)
                {
                    StocktakeMaterials = new List<WmsStocktakeMaterial>();
                }

                sql = new Sql();
                sql.Append(@"Select WmsStocktakeBarcode.*,
                            WmsStocktakeOrderInfo.StocktakeNO,
                            WmsStocktakeOrderInfo.TakeType,
							BasBarcodeInfo.BatchNO,
							BasMaterialInfo.MaterialNO
                            From WmsStocktakeBarcode
							Inner JOin BasBarcodeInfo On WmsStocktakeBarcode.BarcodeNO=BasBarcodeInfo.BarcodeNO
							Inner JOIn BasMaterialInfo ON BasMaterialInfo.MaterialID=BasBarcodeInfo.MaterialID
                            Inner JOin WmsStocktakeOrderInfo 
                            ON WmsStocktakeBarcode.StocktakeID=WmsStocktakeOrderInfo.StocktakeID");
                sql.Where( " WmsStocktakeOrderInfo.StocktakeNO='" + StockTakeNO + "'");
                sql.OrderBy("WmsStocktakeBarcode.BarcodeNO");
                TakeBarcodes = db.Fetch<WmsStocktakeBarcode>(sql);

                sql = new Sql();
                sql.Append(@"Select BasBarcodeInfo.*,
                            BasMaterialInfo.MaterialNO from BasBarcodeInfo
                            Inner Join BasMaterialInfo 
                            ON BasMaterialInfo.MaterialID=BasBarcodeInfo.MaterialID");
                sql.Where("BasBarcodeInfo.BarcodeNO='" + BarcodeNO + "'");

                 Barcode = db.FirstOrDefault<BasBarcodeInfo>(sql);
                if (Barcode == null)
                {
                    Msg = " Barcode NO Is Error";
                    db.AbortTransaction();
                    return null;
                }

                sql = new Sql();
                sql.Append(@"Select BasStorageInfo.*,
                            BasWarehouseInfo.WarehouseNO from BasStorageInfo
                            Inner Join BasWarehouseInfo 
                            ON BasWarehouseInfo.WarehouseID=BasStorageInfo.WarehouseID");
                sql.Where("BasStorageInfo.StorageNO='" + StorageNO + "'");

                BasStorageInfo Storage=db.FirstOrDefault<BasStorageInfo>(sql);


                if (Storage == null)
                {
                    Msg = " Storage NO Is Error";
                    db.AbortTransaction();
                    return null;
                }

                BasLampInfo Lamp = new BasLampInfo();
                if (!string.IsNullOrEmpty(LampNO))
                {
                    sql = new Sql();
                    sql.Append(@"Select BasLampInfo.*
                            Inner Join BasLampInfo 
                            ");
                    sql.Where("BasLampInfo.LampNO='" + LampNO + "'");
                    Lamp = db.FirstOrDefault<BasLampInfo>(sql);
                    if (Lamp == null)
                    {
                        Msg = " Lamp NO Is Error";
                        db.AbortTransaction();
                        return null;
                    }
                }

                int index = -1;
                int abc = -1;
                switch (TaskType)
                {
            
                    case "1":

                        abc = StocktakeMaterials.FindIndex(p => p.StorageNO == StorageNO && p.MaterialNO == Barcode.MaterialNO && p.BatchNO == Barcode.BatchNO);
                        if (abc < 0)
                        {
                            sql = new Sql();
                            sql.Append(@"Select WmsStocktakeOrderInfo.TakeType+'-'+ CONVERT(nvarchar(10), CONVERT(int, SUBSTRING( Max(erpItemNO),5,6))+1) ErpItemNO
                                    From WmsStocktakeMaterial
                                    Inner JOIn WmsStocktakeOrderInfo ON WmsStocktakeOrderInfo.StocktakeID=WmsStocktakeMaterial.StocktakeID
                                    And WmsStocktakeOrderInfo.TakeType='" + OrderInfo.TakeType + @"'
                                    Group BY WmsStocktakeOrderInfo.TakeType");
                            WmsStocktakeMaterial stm = db.FirstOrDefault<WmsStocktakeMaterial>(sql);
                            if (stm == null)
                            {
                                stm = new WmsStocktakeMaterial();
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.FirstQty = Barcode.Qty;
                                stm.FirstTime = BusinessTool.GetDateTime();
                                stm.FirstUser = UserNO;
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                stm.ErpItemNO = OrderInfo.TakeType + "-000001";
                                stm.RState = "1";
                                abc = 0;
                                sb.Append(BusinessTool.GetInsertSql(stm));

                            }
                            else
                            {
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.FirstQty = Barcode.Qty;
                                stm.FirstTime = BusinessTool.GetDateTime();
                                stm.FirstUser = UserNO;
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                stm.RState = "1";
                                abc = 0;
                                sb.Append(BusinessTool.GetInsertSql(stm));
                            }
                            StocktakeMaterials.Add(stm);
                        }
                        else
                        {

                            if (StocktakeMaterials[abc].RState == "0" || StocktakeMaterials[abc].RState == "1")
                            {
                                StocktakeMaterials[abc].RState = "1";
                                StocktakeMaterials[abc].FirstQty += Barcode.Qty;
                                StocktakeMaterials[abc].FirstTime = BusinessTool.GetDateTime();
                                StocktakeMaterials[abc].FirstUser = UserNO;
                                sb.Append(BusinessTool.GetUpdateSql(StocktakeMaterials[abc]));
                            }
                            else
                            {
                                Msg = "Check State Not First";
                                db.AbortTransaction();
                                return null;
                            }

                        }
                         index = TakeBarcodes.FindIndex(p => p.BarcodeNO == BarcodeNO);
                        if (index < 0)
                        {

                            WmsStocktakeBarcode obj = new WmsStocktakeBarcode();
                            obj.BarcodeNO = Barcode.BarcodeNO;
                            obj.FirstQty = Barcode.Qty;
                            obj.FirstTime = BusinessTool.GetDateTime();
                            obj.FirstUser = UserNO;
                            obj.StocktakeID = OrderInfo.StocktakeID;
                            obj.RState = TaskType;
                            obj.ErpItemNO = StocktakeMaterials[abc].ErpItemNO;
                            obj.TakeBarcodeID = Guid.NewGuid().ToString();
                            sb.Append(BusinessTool.GetInsertSql(obj));
                        }
                        else
                        {
                            Msg = " Barcode Is Checked";
                            db.AbortTransaction();
                            return null;
                        }
                        break;
                    case "2":
                        abc = StocktakeMaterials.FindIndex(p => p.StorageNO == StorageNO && p.MaterialNO == Barcode.MaterialNO && p.BatchNO == Barcode.BatchNO);
                        if (abc < 0)
                        {
                            sql = new Sql();
                            sql.Append(@"Select WmsStocktakeOrderInfo.TakeType+'-'+ CONVERT(nvarchar(10), CONVERT(int, SUBSTRING( Max(erpItemNO),5,6))+1) ErpItemNO
                                    From WmsStocktakeMaterial
                                    Inner JOIn WmsStocktakeOrderInfo ON WmsStocktakeOrderInfo.StocktakeID=WmsStocktakeMaterial.StocktakeID
                                    And WmsStocktakeOrderInfo.TakeType='" + OrderInfo.TakeType + @"'
                                    Group BY WmsStocktakeOrderInfo.TakeType");
                            WmsStocktakeMaterial stm = db.FirstOrDefault<WmsStocktakeMaterial>(sql);
                            if (stm == null)
                            {
                                stm = new WmsStocktakeMaterial();
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.SecondQty = Barcode.Qty;
                                stm.SecondTime = BusinessTool.GetDateTime();
                                stm.SecondUser = UserNO;
                                stm.RState = "2";
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                stm.ErpItemNO = OrderInfo.TakeType + "-000001";
                                abc = 0;
                                sb.Append(BusinessTool.GetInsertSql(stm));

                            }
                            else
                            {
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.SecondQty = Barcode.Qty;
                                stm.SecondTime = BusinessTool.GetDateTime();
                                stm.SecondUser = UserNO;
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                abc = 0;
                                stm.RState = "2";
                                sb.Append(BusinessTool.GetInsertSql(stm));
                            }
                            StocktakeMaterials.Add(stm);
                        }
                        else
                        {

                            if (StocktakeMaterials[abc].RState == "2" || (StocktakeMaterials[abc].RState == "1"|| StocktakeMaterials[abc].SecondQty==0))
                            {
                                StocktakeMaterials[abc].SecondQty += Barcode.Qty;
                                StocktakeMaterials[abc].SecondTime = BusinessTool.GetDateTime();
                                StocktakeMaterials[abc].RState = "2";
                                StocktakeMaterials[abc].SecondUser = UserNO;
                                sb.Append(BusinessTool.GetUpdateSql(StocktakeMaterials[abc]));
                            }
                            else
                            {
                                Msg = "Check State Not Second";
                                db.AbortTransaction();
                                return null;
                            }

                        }
                        index = TakeBarcodes.FindIndex(p => p.BarcodeNO == BarcodeNO);
                        if (index < 0)
                        {

                            WmsStocktakeBarcode obj = new WmsStocktakeBarcode();
                            obj.BarcodeNO = Barcode.BarcodeNO;
                            obj.SecondQty = Barcode.Qty;
                            obj.SecondTime = BusinessTool.GetDateTime();
                            obj.SecondUser = UserNO;
                            obj.StocktakeID = OrderInfo.StocktakeID;
                            obj.RState = TaskType;
                            obj.ErpItemNO = StocktakeMaterials[abc].ErpItemNO;
                            obj.TakeBarcodeID = Guid.NewGuid().ToString();
                            sb.Append(BusinessTool.GetInsertSql(obj));
                        }
                        else
                        {
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].RState = TaskType;
                            sb.Append(BusinessTool.GetUpdateSql(TakeBarcodes[index]));
                        }
                        break;
                    case "3":
                        abc = StocktakeMaterials.FindIndex(p => p.StorageNO == StorageNO && p.MaterialNO == Barcode.MaterialNO && p.BatchNO == Barcode.BatchNO);
                        if (abc < 0)
                        {
                            sql = new Sql();
                            sql.Append(@"Select WmsStocktakeOrderInfo.TakeType+'-'+ CONVERT(nvarchar(10), CONVERT(int, SUBSTRING( Max(erpItemNO),5,6))+1) ErpItemNO
                                    From WmsStocktakeMaterial
                                    Inner JOIn WmsStocktakeOrderInfo ON WmsStocktakeOrderInfo.StocktakeID=WmsStocktakeMaterial.StocktakeID
                                    And WmsStocktakeOrderInfo.TakeType='" + OrderInfo.TakeType + @"'
                                    Group BY WmsStocktakeOrderInfo.TakeType");
                            WmsStocktakeMaterial stm = db.FirstOrDefault<WmsStocktakeMaterial>(sql);
                            if (stm == null)
                            {
                                stm = new WmsStocktakeMaterial();
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.FirstQty = Barcode.Qty;
                                stm.FirstTime = BusinessTool.GetDateTime();
                                stm.FirstUser = UserNO;
                                stm.RState = "3";
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                stm.ErpItemNO = OrderInfo.TakeType + "-000001";
                                abc = 0;
                                sb.Append(BusinessTool.GetInsertSql(stm));

                            }
                            else
                            {
                                stm.MaterialNO = Barcode.MaterialNO;
                                stm.TakeMaterialID = Guid.NewGuid().ToString();
                                stm.BatchNO = Barcode.BatchNO;
                                stm.StorageNO = Storage.StorageNO;
                                stm.WarehouseNO = Storage.WarehouseNO;
                                stm.FirstQty = Barcode.Qty;
                                stm.FirstTime = BusinessTool.GetDateTime();
                                stm.FirstUser = UserNO;
                                stm.StocktakeID = OrderInfo.StocktakeID;
                                abc = 0;
                                stm.RState = "3";
                                sb.Append(BusinessTool.GetInsertSql(stm));
                            }
                            StocktakeMaterials.Add(stm);
                        }
                        else
                        {

                            if (StocktakeMaterials[abc].RState == "3" || (StocktakeMaterials[abc].RState == "2" || StocktakeMaterials[abc].ThreeQty == 0))
                            {
                                StocktakeMaterials[abc].ThreeQty += Barcode.Qty;
                                StocktakeMaterials[abc].ThreeTime = BusinessTool.GetDateTime();
                                StocktakeMaterials[abc].RState = "3";
                                StocktakeMaterials[abc].ThreeUser= UserNO;
                                sb.Append(BusinessTool.GetUpdateSql(StocktakeMaterials[abc]));
                            }
                            else
                            {
                                Msg = "Check State Not Three Step";
                                db.AbortTransaction();
                                return null;
                            }

                        }
                        index = TakeBarcodes.FindIndex(p => p.BarcodeNO == BarcodeNO);
                        if (index < 0)
                        {

                            WmsStocktakeBarcode obj = new WmsStocktakeBarcode();
                            obj.BarcodeNO = Barcode.BarcodeNO;
                            obj.SecondQty = Barcode.Qty;
                            obj.SecondTime = BusinessTool.GetDateTime();
                            obj.SecondUser = UserNO;
                            obj.StocktakeID = OrderInfo.StocktakeID;
                            obj.RState = TaskType;
                            obj.ErpItemNO = StocktakeMaterials[abc].ErpItemNO;
                            obj.TakeBarcodeID = Guid.NewGuid().ToString();
                            sb.Append(BusinessTool.GetInsertSql(obj));
                        }
                        else
                        {
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].SecondQty = Barcode.Qty;
                            TakeBarcodes[index].RState = TaskType;
                            sb.Append(BusinessTool.GetUpdateSql(TakeBarcodes[index]));
                        }
                        break;


                }


                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                }
                db.CompleteTransaction();


            }
            catch (Exception ex)
            {
                Msg = ex.ToString();
                db.AbortTransaction();
            }
            sql = new Sql();
            sql.Append(@"Select WmsStocktakeBarcode.*,
                        WmsStocktakeMaterial.MaterialNO,WmsStocktakeMaterial.BatchNO
                        From WmsStocktakeBarcode
                        Inner JOIn WmsStocktakeMaterial ON WmsStocktakeBarcode.ErpItemNO=WmsStocktakeBarcode.ErpItemNO");
            sql.Where("WmsStocktakeMaterial.StorageNO='" + StorageNO + "'");
            sql.Where("WmsStocktakeMaterial.MaterialNO='" + Barcode.MaterialNO + "'");
            sql.OrderBy("WmsStocktakeBarcode.BarcodeNO");
            TakeBarcodes = db.Fetch<WmsStocktakeBarcode>(sql);


            return TakeBarcodes;
        }

        /// <summary>
        /// 获取盘点类型
        /// </summary>
        /// <returns></returns>
        public List<TakeTypeInfo>  GetTakeTypeList()
        {
            List<TakeTypeInfo> list = new List<TakeTypeInfo>();
            TakeTypeInfo obj = new TakeTypeInfo();
            obj.Name = "M" + DateTime.Now.Year.ToString().Substring(2,2);
            list.Add(obj);
            obj = new TakeTypeInfo();
            obj.Name = "A" + DateTime.Now.Year.ToString().Substring(2, 2);
            list.Add(obj);
            obj = new TakeTypeInfo();
            obj.Name = "M" + DateTime.Now.AddYears(-1).Year.ToString().Substring(2, 2);
            list.Add(obj);
            obj = new TakeTypeInfo();
            obj.Name = "A" + DateTime.Now.AddYears(-1).Year.ToString().Substring(2, 2);
            list.Add(obj);
            obj = new TakeTypeInfo();
            obj.Name = "M" + DateTime.Now.AddYears(1).Year.ToString().Substring(2, 2);
            list.Add(obj);
            obj = new TakeTypeInfo();
            obj.Name = "A" + DateTime.Now.AddYears(1).Year.ToString().Substring(2, 2);
            list.Add(obj);
            return list;
        }

    }
    public class TakeTypeInfo
    {
        /// <summary>
        /// 盘点类型
        /// </summary>
        public string Name { get; set; }
    }


}
